package com.codingday.app;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class TODOListAdapter extends ArrayAdapter<TODO>{
		
		private Context mContext;
		ArrayList<TODO> todolist;
		LayoutInflater mInflater;
		int mLayout;
		View mView;
		// ARGB
		private int[] colors = new int[] { 0xFF005EBC };	
		private int[] colorLabels = new int[] {0xFF27ADC5, 0xFF282C61};
		private int[] textColors = new int[] {0xFFC5F275};

		public TODOListAdapter(Context context, int textViewResourceId, ArrayList<TODO> todolist) {
			super(context, textViewResourceId, todolist);
			this.mContext = context;
			this.todolist = todolist;
			Log.i("TODOAdapter","Constructor : " + todolist.size());
			// TODO Auto-generated constructor stub
			this.mLayout = textViewResourceId;
			this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			//return super.getCount();
			return todolist.size();
		}



		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			//return super.getItemId(position);
			return position;
		}



		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			mView = convertView;
			TODOViewHolder todoViewHolder;
			
			if(mView == null){
				//mView = mInflater.inflate(R.layout.row_todo, null);
				mView = mInflater.inflate(mLayout, parent, false);
			
				todoViewHolder = new TODOViewHolder();
				todoViewHolder.colorLabel = (TextView) mView.findViewById(R.id.todo_color_label);
				todoViewHolder.titleText = (TextView) mView.findViewById(R.id.todotitle);
				//todoViewHolder.localText = (TextView) mView.findViewById(R.id.todo_location_title);
				//todoViewHolder.enableToggleButton = (ToggleButton) mView.findViewById(R.id.todo_toggle_enable_button);
				todoViewHolder.enableCheckBox = (CheckBox) mView.findViewById(R.id.todo_check_enable_button);
				
				mView.setTag(todoViewHolder);
			}else{
				todoViewHolder = (TODOViewHolder) mView.getTag();
			}
			
			//mView = vi.inflate(R.layout.row_todo, null);
			final TODO todo = todolist.get(position);
			
			if(todo != null){
				//Log.i("getView",""+todo.getTitle());
				//TextView ttv = (TextView) mView.findViewById(R.id.todotitle);
				//TextView ltv = (TextView) mView.findViewById(R.id.todo_location_title);
				//if(ttv != null && ltv != null){
					int colorIndex = position % colors.length;
					int textColorIndex = position % textColors.length;
					int colorLabelIndex = position % colorLabels.length;
					
					mView.setBackgroundColor(colors[colorIndex]);
					todoViewHolder.colorLabel.setBackgroundColor(colorLabels[colorLabelIndex]);
					
					//Log.i("Title Length - ","Position: "+position +", Length:"+ todo.getTitle().length());
					//ttv.setText(todo.getTitle());
					//ltv.setText("home");
					
					if(todo.isCompleted()){
						todoViewHolder.titleText.setPaintFlags(todoViewHolder.titleText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
						todoViewHolder.enableCheckBox.setEnabled(false);
					}
					todoViewHolder.titleText.setText(todo.getTitle());	
					todoViewHolder.titleText.setTextColor(textColors[textColorIndex]);
					//todoViewHolder.localText.setText("Home - "+position);
					//todoViewHolder.localText.setText("[ #"+todo.getID()+" ]");
					//todoViewHolder.enableToggleButton.setChecked(todo.isEnabled());
					todoViewHolder.enableCheckBox.setChecked(todo.isEnabled());
					
					
					final int pos = position;

					//todoViewHolder.titleText.setClickable(true);
					
					todoViewHolder.titleText.setOnClickListener(new OnClickListener(){

						@Override
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							//Toast.makeText(mContext, "touch : "+pos, 1).show();
							long ID = todo.getID();
							goToViewToDoActivity(ID);
						}
						
					});
					
					
					//todoViewHolder.enableToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
					todoViewHolder.enableCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

						@Override
						public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
							// TODO Auto-generated method stub
							if(isChecked){
								todolist.get(pos).Enable();
							}else{
								todolist.get(pos).Disable();
							}
						}
						
					});
					//titleV.setText("hello" + position);
					//titleV.setT
				//}
			}
			return mView;
		}
		
		
		public void goToViewToDoActivity(long ID){
			Intent intent = new Intent(mContext, TODOView.class);
			//intent.putExtra("TODOID", ID);
			intent.putExtra("TODOID", ID);
			Log.i("TODOListView","goToViewToDoActivity - ID:"+ID);
			((Activity) mContext).startActivityForResult(intent,2);		
		}
}
